package com.kolaxy.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * <p>todo</p>
 *
 * @author zhourunjin
 * @date 2020/6/9 12:53
 */
public class TestNio {

    public static void main(String[] args) throws Exception {
        copy();
    }

    public static void write() throws IOException {
        FileOutputStream outputStream = new FileOutputStream("basic.txt");

        FileChannel channel = outputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        byteBuffer.put("hello world".getBytes());

        byteBuffer.flip();

        channel.write(byteBuffer);

        channel.close();

    }


    public static void read() throws Exception {

        FileInputStream fileInputStream = new FileInputStream("basic.txt");
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        fileInputStream.getChannel().read(buffer);
        System.out.println(new String(buffer.array()));

    }

    public static void copy() throws Exception {
        FileInputStream inputStream = new FileInputStream("basic.txt");

        FileOutputStream outputStream = new FileOutputStream("aa.txt");

        FileChannel inputStreamChannel = inputStream.getChannel();

        FileChannel outputStreamChannel = outputStream.getChannel();

        outputStreamChannel.transferFrom(inputStreamChannel, 0, inputStreamChannel.size());


    }
}
